package com.codefactory.aps.classes;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;

public class LogManager {

    private static File log;
    private static PrintWriter out;
    private static boolean isLogBlocked;

    public static void createLog() {

        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String logNow = formatter.format(currentDate.getTime());

        log = new File("logger/" + logNow + ".txt");

        try {
            log.createNewFile();
            out = new PrintWriter(log);
            return;
        } catch (IOException ex) {
        }

        try {
            log.createNewFile();
            out = new PrintWriter(log);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "The logger could not be started.\nWARNING: Data will not be collected for this session.\nWarning Message: " + ex.getMessage(), "Logger Warning", JOptionPane.WARNING_MESSAGE);
            isLogBlocked = true;
        }

    }

    public static void log(String log) {

        if (!isLogBlocked) {

            Calendar currentDate = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String messagePrecedent = formatter.format(currentDate.getTime());
            
            out.println(messagePrecedent + " - " + log);

        }

    }
    
    public static void closePrintWriter(){
        
        out.close();
        
    }
    
}